package com.example.zyfypt516wh.activities;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.adapter.FocusAdapter;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.model.FocusModel;

import java.util.List;

/**
 * 我关注的用户列表
 */
public class FocusActivity extends AppCompatActivity {

    private List<FocusResult> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;//显示布局效果
    private int page = 1; // 代表页数， 并初始化为 1， 代表第 1 页。
    private int lastVisibleItemPosition;//滚动刷新时， 最后一条可见条目的位置
    private FocusAdapter adapter;

    private SharedPreferences sp;

    String type, id,userid,sessionID,collecStatus,followStatus;
    String focusType="userfocus";

    private FocusListener listener = new FocusListener() {

        @Override
//        public void onResponse(List<FocusResult> beanlist) {
        public void onResponse(List<FocusResult> beanlist) {
            if (page == 1)//第一页直接赋值
            {
                list = beanlist;
            } else {//其他页需要更新替换
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);
        }

        @Override
        public void onResponse(String res) {

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FocusActivity.this, "失败： " + msg, Toast.LENGTH_SHORT).show();
            System.out.println("--" + msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow);

        sp=this.getSharedPreferences("user",MODE_PRIVATE);
        sessionID=sp.getString("sessionID","");
        initRecyclerView();
        FocusModel model=new FocusModel();
        model.myFocus(focusType, page, sessionID, listener);
    }



    private void initRecyclerView() {
        recyclerView = findViewById(R.id.rv_follow);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //每个 item 如果是确定高度， 设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter = new FocusAdapter(this,sessionID);
        recyclerView.setAdapter(adapter);
        //列表滚动刷新
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (list != null) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                        page += 1;
                        //再次实例化 FocusModel， 调用方法获取网络数据， 请求新一页数据
                        FocusModel model=new FocusModel();
                        model.myFocus(focusType, page, sessionID, listener);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位

            }
        });
    }

}